﻿$(document).ready(function () {
    $("#chat").animate({ scrollTop: 999999 }, 800);

    $(document).keypress(function (event) {
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (keycode == '13') {
            $(".notification").remove();
            var text = document.getElementById("enter").value.trim();
            if (text != "") {
                var temp = document.getElementById("chat").innerHTML;
                document.getElementById("chat").innerHTML += "<div class='bubble bubble-alt w3-teal'>" + text + "</div></br></br></br>";
                document.getElementById("enter").value = "";

                $("#chat").animate({ scrollTop: 999999 }, 800);
                var xhttp;
                xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function () {
                    if (xhttp.readyState == 4 && xhttp.status == 200) {

                    }
                };
                xhttp.open("GET", "../ajax/asp/add_chat.aspx?text=" + text, true);
                xhttp.send();
                //getMessenges();
            }
        }
    });

    $("#btn_send").on("click", function () {
        $(".notification").remove();
        var text = document.getElementById("enter").value.trim();
        if (text != "") {
            document.getElementById("chat").innerHTML += "<div class='bubble bubble-alt w3-teal'>" + text + "</div></br></br></br>";
            document.getElementById("enter").value = "";

            $("#chat").animate({ scrollTop: 999999 }, 800);
            var xhttp;
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (xhttp.readyState == 4 && xhttp.status == 200) {

                }
            };
            xhttp.open("GET", "../ajax/asp/add_chat.aspx?text=" + text, true);
            xhttp.send();
            //getMessenges();
        }
    });

    $(".mute-btn").on("click", function () {
        if ($(this).hasClass("fa-volume-up")) {
            $(this).removeClass("fa-volume-up");
            $(this).addClass("fa-volume-off");
            document.getElementById("mute_chat").innerHTML = "1";
        }
        else {
            $(this).removeClass("fa-volume-off");
            $(this).addClass("fa-volume-up");
            document.getElementById("mute_chat").innerHTML = "0";
        }
    });
    $(".chat-icon").on("click", function () {
        $(".notification").remove();
        var iconsrc = this.src;
        var icon = iconsrc.split("/");
        var src = "IT'sSQTicons_" +icon[iconsrc.split("/").length-1];
        if (src != "") {
            document.getElementById("chat").innerHTML += "<div class='bubble bubble-alt w3-teal'><img src='" + iconsrc + "' alt='icons' /></div></br></br></br>";
            document.getElementById("enter").value = "";
            $("#chat").animate({ scrollTop: 999999 }, 800);
            var xhttp;
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (xhttp.readyState == 4 && xhttp.status == 200) {

                }
            };
            xhttp.open("GET", "../ajax/asp/add_chat.aspx?text=" + src, true);
            xhttp.send();
        }
    });
    setInterval(function () {
        var xhttp;
        var last_id_old = document.getElementById("last_chat_id").innerHTML.trim();

        var last_id_new = "";
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("last_chat_id_new").innerHTML = xhttp.responseText;
                last_id_new = document.getElementById("last_chat_id_new").innerHTML.trim();

                if (last_id_new != last_id_old) {
                    getMessenges();
                    document.getElementById("last_chat_id").innerHTML = last_id_new;
                }
            }
        };
        xhttp.open("GET", "../ajax/asp/get_last_chat.aspx", true);
        xhttp.send();
    }, 1500);

    function getMessenges() {
        $(".notification").remove();
        var mute = document.getElementById("mute_chat").innerHTML;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("chat").innerHTML = xhttp.responseText;
                $(document).ready(function () {
                    $('[data-toggle="tooltip"]').tooltip();
                    $("#chat").animate({ scrollTop: 999999 }, 800);
                });
            }
        };
        xhttp.open("GET", "../ajax/asp/getMessenges.aspx?mute="+mute, true);
        xhttp.send();
        $(".notification").remove();
    }
});